//
//  CZVisitorView.swift
//  wuJunQianWeiBo
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 wujunqian. All rights reserved.
//

import UIKit

//定义代理协议
protocol CZVisitorViewDelegate: NSObjectProtocol
{
    //点击注册按钮
    func didRegisterButtonClick()
    //点击登录按钮
    func didLoginButtonClick()
}

class CZVisitorView: UIView {
    
    //添加代理属性，默认为strong，有可能没有
    weak var delegate : CZVisitorViewDelegate?
    
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        //设置背景颜色
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     按钮点击事件
     */
    @objc private func registerClick() {
        // 调用代理通知控制器按钮被点击
        // delegate有值就调用visitorViewRegisterClick
        // delegate没有值就不执行visitorViewRegisterClick
        delegate?.didRegisterButtonClick()
    }
    
    func loginClick() {
        // view是不能modal出来控制器, 只有控制器才能modal出来
        // 点击事件需要传给控制器
        
        delegate?.didLoginButtonClick()
    }
    
    /**
     不同页面显示不同信息
     */
    func setupInfo(imageName: String,text: String)
    {
        //给图片和文字赋值
        iconView.image = UIImage(named: imageName)
        textLabel.text = text
        //隐藏小房子
        houseView.hidden = true
        //遮盖放到最下面
        sendSubviewToBack(coverView)
        
    }
    
    //转轮旋转
    func startRotationAnimation()
    {
        //动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //从0弧度开始转
        animation.fromValue = (0)
        //转360弧度
        animation.toValue = (2 * M_PI)
        //4秒一圈
        animation.duration = 4
        //无限转
        animation.repeatCount = MAXFLOAT
        //切换到其他tabBar或者退出到桌面时不会停止动画
        animation.removedOnCompletion = false
        //添加动画
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }
    //准备UI
    private func prepareUI()
    {
        //添加子控件,可以省略self
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //要取消autoresizing
        //用autolayout约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        //转轮centerX和centerY的约束
        //添加约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
//        //遮罩约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //房子X和Y的约束
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息label的X和iconView的centerX重合
        self.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //顶部距离iconView的底部16
        self.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度224
        self.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        // 顶部距离textLabel 16
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //左边和textLabel的左边重合
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //宽度100，高度35
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 顶部距离textLabel 16
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //右边和textLabel的右边重合
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //宽度100，高度35
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    //转轮子控件懒加载
    private lazy var iconView: UIImageView =
    {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        let iconView = UIImageView(image: image)
        return iconView
    }()
    
    //遮罩
    private lazy var coverView: UIImageView =
    {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    //房子
    private lazy var houseView: UIImageView =
    {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    //文字标签
    private lazy var textLabel: UILabel =
    {
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
//        label.backgroundColor = UIColor.blackColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton =
    {
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //监听点击按钮事件
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton =
    {
        let button = UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //监听点击按钮事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()

}
